﻿using Liu_Project.Dal.Admin;
using LSL.SqlClient;
using Newtonsoft.Json;
using System.Data;
using Liu_Project.Utils;

namespace Liu_Project.Web.Admin.Handler.Personal
{
    /// <summary>
    /// EntRoleHand 的摘要说明
    /// </summary>
    public class EntRoleHand : HandlerBaseAsync
    {
        private readonly EntRoleDal dal = new EntRoleDal();

        public void LoadPage()
        {
            _logOperation = "角色管理-获取列表数据";
            var sqlWhere = ExpressionMethod.GetSqlWhere(_context);

            var data = dal.GetRoleList(sqlWhere, "RoleId", "DESC", page, pageSize, ref total);

            _context.Response.Write(CommonFunction.SuccessJson(data, total));
        }

        public void Save()
        {
            _logOperation = "角色管理-保存";
            var _state = DataConvert.ToString(_context.Request["_state"]);
            var RoleId = DataConvert.ToString(_context.Request["RoleId"], "0");
            var RoleName = DataConvert.ToString(_context.Request["RoleName"], "");

            dal.Save(_state, RoleId, RoleName);

            _context.Response.Write(CommonFunction.SuccessJson());
        }

        public void Delete()
        {
            _logOperation = "角色管理-删除";
            var Data = DataConvert.ToString(_context.Request["Data"], "0");

            new SQLHelper().ExecuteNonQuery("DELETE FROM EntRole WHERE RoleId IN (" + Data + ")");

            _context.Response.Write(CommonFunction.SuccessJson());
        }

        /// <summary>
        /// 加载授权菜单
        /// </summary>
        public void LoadList()
        {
            _logOperation = "角色管理-授权菜单";
            var _Id = DataConvert.ToString(_context.Request["Id"], "0");
            var _type = DataConvert.ToString(_context.Request["Type"]);
            var _IsVisiable = DataConvert.ToString(_context.Request["IsVisiable"], "1");

            var data = dal.GetRoleMenuList(_type, _Id, _IsVisiable);

            _context.Response.Write(CommonFunction.SuccessJson(data));
        }

        /// <summary>
        /// 保存权限菜单
        /// </summary>
        public void SaveMenuCapability()
        {
            _logOperation = "角色管理-保存授权菜单";
            var _data = DataConvert.ToString(_context.Request["Data"], "[]");
            var _Id = DataConvert.ToString(_context.Request["Id"], "0");
            var _ownerType = DataConvert.ToString(_context.Request["Type"]);

            var Data = JsonConvert.DeserializeObject<DataTable>(_data);
            if (Data.Rows.Count == 0)
            {
                _context.Response.Write(CommonFunction.ErrorJson("没有要保存的数据!"));
                return;
            }
            dal.SaveMenuCapability(_ownerType, _Id, Data);

            _context.Response.Write(CommonFunction.SuccessJson());
        }

        /// <summary>
        /// 加载授权菜单
        /// </summary>
        public void LoadUserList()
        {
            _logOperation = "用户管理-授权菜单";
            var roleId = DataConvert.ToString(_context.Request["Id"], "0");
            var data = dal.GetRoleUserList(roleId, "UserNo", "DESC", page, pageSize, ref total);

            _context.Response.Write(CommonFunction.SuccessJson(data, total));
        }

        /// <summary>
        /// 用户角色列表
        /// </summary>
        public void GetUserRoleList()
        {
            _logOperation = "角色管理-用户列表";
            var Id = DataConvert.ToString(_context.Request["Id"], "0");
            var data = dal.GetUserRoleList(Id);

            _context.Response.Write(CommonFunction.SuccessJson(data));
        }
    }
}